package org.bikelab.se3.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class FileCompressUtil {

	private static ArrayList<String> getfiles(String path) {
		ArrayList<String> filepaths = new ArrayList<String>();
		File files = new File(path);

		File[] fs = files.listFiles();
		for (File f : fs) {
			filepaths.add(f.getPath());

		}

		return filepaths;

	}

	/**
	 * 
	 * 
	 * @param path
	 * @throws IOException
	 */
	public static void decompress(String inputpath, String outputpath)
			throws IOException {
		
		File fn = new File(inputpath);

		try {
			String name = fn.getName().substring(0,
					fn.getName().indexOf(".gz"));
			GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputpath));
			FileOutputStream out = new FileOutputStream(outputpath + "/"
					+ name);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}

			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
//		System.out.println("start decompress...");
//		ArrayList<String> files = (getfiles(inputpath));
//		int counter = 1;
//		for (String f : files) {
//			System.out.println("processing " + counter + "/" + files.size());
//			File fn = new File(f);
//
//			try {
//				String name = fn.getName().substring(0,
//						fn.getName().indexOf(".gz"));
//				GZIPInputStream in = new GZIPInputStream(new FileInputStream(f));
//				FileOutputStream out = new FileOutputStream(outputpath + "/"
//						+ name);
//
//				byte[] buf = new byte[1024];
//				int len;
//				while ((len = in.read(buf)) > 0) {
//					out.write(buf, 0, len);
//				}
//
//				in.close();
//				out.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//				System.exit(1);
//			}
//
//			// FileWriter fw= new FileWriter(outputpath+"/"+name);
//			// BufferedWriter tempbfw= new BufferedWriter(fw);
//			//
//			// try {
//			//
//			// BufferedReader gzipReader = new BufferedReader(
//			// new InputStreamReader(new GZIPInputStream(
//			// new FileInputStream(f))));
//			//
//			// while (gzipReader.ready()) {
//			// String line=gzipReader.readLine();
//			// tempbfw.write(line);
//			// tempbfw.write(System.getProperty("line.separator"));
//			// }
//			//
//			// gzipReader.close();
//			//
//			// } catch (FileNotFoundException e) {
//			// e.printStackTrace();
//			// } catch (IOException e) {
//			// e.printStackTrace();
//			// }
//			// tempbfw.close();
//			counter++;
//		}
//		System.out.println("finish decompress!");
	}

	/**
	 * 
	 * 
	 * @param path
	 * @throws IOException
	 */

	public void compress(String inputpath, String outputpath)
			throws IOException {
		System.out.println("start compress...");
		ArrayList<String> files = (getfiles(inputpath));
		int counter = 1;
		for (String f : files) {
			File fn = new File(f);
			System.out.println("processing " + counter + "/" + files.size());
			String name = fn.getName() + ".gz";
			try {
				GZIPOutputStream out = new GZIPOutputStream(
						new FileOutputStream(outputpath + "/" + name));

				FileInputStream in = new FileInputStream(f);
				byte[] buf = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);

				}
				in.close();

				out.finish();
				out.close();

			} catch (IOException e) {
				e.printStackTrace();
				System.exit(1);
			}

			counter++;
		}
		System.out.println("finish compress!");

	}
}
